package com.apes.fn.server.vehicleFile.server;

import com.apes.fn.server.vehicleFile.model.VehicleFileAccount;
import com.apes.fn.server.vehicleFile.model.VehicleFileArea;
import com.apes.fn.server.vehicleFile.model.VehicleFileAreaEnum;
import com.apes.fn.server.vehicleFile.model.VehicleFileAreaItem;
import com.apes.fn.server.vehicleFile.repository.VehicleFileAccountRepository;
import com.apes.fn.server.vehicleFile.server.vehicleFileUploadProcess.CommonUploadProcess;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @program: apes-server
 * @description:
 * @author: czs
 * @create: 2023-11-02 14:13
 **/
@Service("vehicleFileAccountServer")
public class VehicleFileAccountServer extends DomainService {

    @Autowired
    private VehicleFileAccountRepository vehicleFileAccountRepository;

    private VehicleFileAccount save(SimpleRequest request) {
        VehicleFileAccount vehicleFileAccount = request.getO(VehicleFileAccount.class);
        String password = vehicleFileAccount.getPassword();
        if (vehicleFileAccount.getBelongDept() == null) throw new RuntimeException("有必填项未完成");
        if (vehicleFileAccount.getVehicleFileArea() == null) throw new RuntimeException("有必填项未完成");
        if (vehicleFileAccount.getAccount() == null || vehicleFileAccount.getAccount().trim().equals(""))
            throw new RuntimeException("有必填项未完成");
        if (password == null || password.trim().equals("")) throw new RuntimeException("有必填项未完成");
        int length = password.length();
        int length1 = password.replace(" ", "").length();
        if (length != length1) throw new RuntimeException("密码不允许包含空格");
        createOrUpdateToken(vehicleFileAccount);
        Dept belongDept = vehicleFileAccount.getBelongDept();
        vehicleFileAccount.setCompany(belongDept.getCompany());
        return vehicleFileAccountRepository.saveAndFlush(vehicleFileAccount);
    }

    private void createOrUpdateToken(VehicleFileAccount vehicleFileAccount) {
        VehicleFileArea vehicleFileArea = vehicleFileAccount.getVehicleFileArea();
        CommonUploadProcess commonUploadProcess = VehicleFileAreaEnum.nameFind(vehicleFileArea.getApiType());
        if (commonUploadProcess!=null){
            String newToken = commonUploadProcess.getToken(vehicleFileAccount);
            if (!newToken.equals(vehicleFileAccount.getToken())){
                vehicleFileAccount.setToken(null);
                vehicleFileAccount.setTokenStartDate(null);
                vehicleFileAccount.setTokenEndDate(null);
                commonUploadProcess.checkTokenValid(vehicleFileAccount);
            }
        }else {
            vehicleFileAccount.setToken(null);
            vehicleFileAccount.setTokenStartDate(null);
            vehicleFileAccount.setTokenEndDate(null);
        }
    }

    private VehicleFileAccount update(SimpleRequest request){
        VehicleFileAccount vehicleFileAccount = request.getO(VehicleFileAccount.class);
        createOrUpdateToken(vehicleFileAccount);

        return vehicleFileAccountRepository.saveAndFlush(vehicleFileAccount);
    }

    private Map delete(SimpleRequest request){
        VehicleFileAccount vehicleFileAccount = request.getO(VehicleFileAccount.class);
//        VehicleFileAccount.setDeleted(true);
        vehicleFileAccount.setToken(null);
        vehicleFileAccount.setTokenStartDate(null);
        vehicleFileAccount.setTokenEndDate(null);
        vehicleFileAccountRepository.delete(vehicleFileAccount);
        return MapUtil.mapper("success",true);
    }

//    @OutboxSubscriber(name = "(浙江)车辆健康档案创建")
    private void createAccount(VehicleFileAccount vehicleFileAccount){
        VehicleFileArea vehicleFileArea = vehicleFileAccount.getVehicleFileArea();
        VehicleFileAreaItem register = vehicleFileArea.findItemByApiType("register");
        if (register==null)return;
        CommonUploadProcess commonUploadProcess = VehicleFileAreaEnum.nameFind(vehicleFileAccount.getVehicleFileArea().getApiType());
        commonUploadProcess.register(vehicleFileAccount);

    }
}
